package com.epfresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.epfresh.R;
import com.epfresh.adapter.CommentNewAdapter;
import com.epfresh.api.entity.*;
import com.epfresh.api.entity.RequestTag;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.T;
import com.epfresh.bean.CommentLoad;
import com.epfresh.global.BaseActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class CommentRequireAllActivity extends BaseActivity {

	private String orderId;

	private ListView mListView;
	private ArrayList<CommentLoad.StoreListBean> newList = new ArrayList<>();

	CommentNewAdapter adapter;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (savedInstanceState != null) {
			getIntent().putExtra("orderId", savedInstanceState.getString("orderId"));
		}
		if (getIntent().getStringExtra("orderId") != null) {
			orderId = getIntent().getStringExtra("orderId");
		} else {
			T.toast("没收到id");
			finish();
		}
		initView();
		requestCommentLoad();


	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putString("orderId", orderId);
		super.onSaveInstanceState(outState);
	}

	void initView(){
		setContentView(R.layout.activity_comment_new);
		mListView = (ListView) findViewById(R.id.lv_comment_new);
		baseback.setVisibility(View.VISIBLE);
		toolbarTitle.setText("获得的评价");
		rightbtn.setVisibility(View.VISIBLE);
		adapter = new CommentNewAdapter(this, newList);
		mListView.setAdapter(adapter);
		adapter.setOnItemClickListener(this);
	}
/*	private PtrMdFrameLayout ptrFrameLayout;
	private PtrHandler ptrHandler = new PtrHandler() {
		@Override
		public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
			return PtrDefaultHandler.checkContentCanBePulledDown(frame, mListView, header);
		}

		@Override
		public void onRefreshBegin(PtrFrameLayout frame) {
			refresh();
		}
	};


	public void refresh() {
		if (mListView != null) {
			//mListView.resetNoMore();
		}
		requestCommentLoad();
	}*/
	private RequestTag requestTag = new RequestTag();

	public void requestCommentLoad() {

		RequestEntityPurchase<Map<String, Object>> entity = new RequestEntityPurchase<>();
		Map<String, Object> map = new HashMap<>();
		map.put("orderId", orderId);
		entity.setCmd(com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_order_order_commentLoad);
		entity.setParameters(map);
		request(entity, requestTag, onRequestListener);

	}

	OnRequestListener<CommentLoad> onRequestListener = new OnRequestListener<CommentLoad>() {
		@Override
		public CommentLoad jsonToObj(String responseStr) {
			return new Gson().fromJson(responseStr, new TypeToken<CommentLoad>() {
			}.getType());
		}

		@Override
		public void onFail(int failCode, Object arg, Object tag) {
			hideProgressDialog();
			//updateView(tag);
			switch (failCode) {
				case HttpRequest.FAIL_NOT_FOUND:
					showExceptionView(0, null, 0);
					break;
				case HttpRequest.FAIL_NO_NET:
					showExceptionView(0, null, 0);
					break;
				case HttpRequest.FAIL_NET_FAULT:
					showExceptionView(0, null, 0);
				default:
					showExceptionView(0, null, 0);
					//showReload();
					break;
			}
		}

		@Override
		public void onResponseError(int failCode, Object arg, Object tag) {
			//updateView(tag);
			showMainView();
		}

		@Override
		public void onStart(Object tag) {

			mListView.setVisibility(View.VISIBLE);
		}

		@Override
		public void onResponse(ResponseEntity<CommentLoad> response, Object tag) {
			hideProgressDialog();
			showMainView();
			CommentLoad goodsResponse = response.getResponseElement();
			if (goodsResponse != null && goodsResponse.getStoreList().size() > 0) {
				newList.addAll(goodsResponse.getStoreList());
				adapter.notifyDataSetChanged();
			}else {
				toast("信息有误，请稍后重试");
				finish();
			}

		}
	};

/*
	private boolean updateView(Object tag) {
		if (tag != null) {
			RequestTag requestTag = (RequestTag) tag;
			if (requestTag.action == 0) {
				ptrFrameLayout.refreshComplete();
				return true;
			} else {

			}
		} else {
			ptrFrameLayout.refreshComplete();
		}
		return false;
	}
*/


	public void onClick(View v) {
		if(v.getId()==R.id.baseback) {
			finish();
			return;
		}
		if(v.getId()==R.id.rightbtn) {
			submit();
			return;
		}
		int position = (Integer) v.getTag(R.id.item_key_position);
		switch (v.getId()) {
			case R.id.tv_comment_go:
				Intent evaluate = new Intent(this, CommentGoodsActivity.class);
				//evaluate.putExtra("orderId",list.get(position).getOrderId());
				evaluate.putExtra("goodsList",(Serializable) newList.get(position).getStoreItemList());
				evaluate.putExtra("storeBean",newList.get(position));
				startActivityForResult(evaluate,0x001);
				break;
			case R.id.iv_comment_1:
			case R.id.iv_comment_2:
			case R.id.iv_comment_3:
				newList.get(position).setScore((Integer) v.getTag(R.id.item_key_status));
				adapter.notifyDataSetChanged();
				break;


		}
	}

	public void submit(){
		toast(""+"submit");
		Log.e("________","submit_______");
	}
}
